package com.cyan.app.workflow.activity.SFC;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

import com.cyan.app.barcode.CaptureActivity;
import com.cyan.app.workflow.AppContext;
import com.cyan.app.workflow.R;
import com.cyan.app.workflow.base.activity.BaseActivity;
import com.cyan.app.workflow.util.AppUtils;

public class MannoInfoActivity extends BaseActivity {
    public static final String TAG = "MannoInfoActivity";
    private TextView mTextViewManno;
    private Button mButton;
    private String manno ;
    private WebView mWebView1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webpage);
        initView();
        manno=getIntent().getStringExtra("Manno");
        if(!manno.isEmpty()) {
            mTextViewManno.setText(manno);
            initData();
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x0002 && resultCode == RESULT_OK) {
            String result = "";
            if (data != null) {
                result = data.getStringExtra("result");
            }
            mTextViewManno.setText(String.valueOf(result));
            manno = String.valueOf(result);
            initData();
        }
    }
    private void initData() {
            final String mannoUrl = "http://172.17.17.103:809/SFC/MannoInfo.htm?Manno=" + manno;
            mWebView1 = (WebView) findViewById(R.id.web);
            mWebView1.getSettings().setJavaScriptEnabled(true);
            mWebView1.getSettings().setDomStorageEnabled(true);
            mWebView1.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);

            mWebView1.getSettings().setSupportZoom(true);
            mWebView1.getSettings().setBuiltInZoomControls(true);

            mWebView1.getSettings().setUseWideViewPort(true);
            mWebView1.getSettings().setLoadWithOverviewMode(true);

            if (android.os.Build.VERSION.SDK_INT > 10) {
                mWebView1.getSettings().setDisplayZoomControls(false);
            }
            mWebView1.loadUrl(mannoUrl);
            mWebView1.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        view.loadUrl(mannoUrl);
                        return true;
                }
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    super.onPageStarted(view, url, favicon);
                }
                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                }

                @Override
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    super.onReceivedError(view, errorCode, description, failingUrl);
                    String dataString = AppUtils.getFromAssets(AppContext.getInstance(), "404.html");
                    view.loadDataWithBaseURL("404.html", dataString, "text/html", "UTF-8", "");
                }
            });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    private void initView(){
        titleBar();
        mTextViewManno = (TextView) findViewById(R.id.top_search);
        mTextViewManno.setInputType(InputType.TYPE_NULL);
        mTextViewManno.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(MannoInfoActivity.this, CaptureActivity.class), 0x0002);
            }
        });
    }

    private void titleBar() {
        Button back = (Button) findViewById(R.id.top_title_back);
        back.getBackground().setAlpha(255);// 透明度0~255透明度值 ，值越小越透明
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回按钮
                MannoInfoActivity.this.setResult(RESULT_OK);
                finish();
            }
        });
        mButton = (Button)findViewById(R.id.top_title_refres_with_icon);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(MannoInfoActivity.this, CaptureActivity.class), 0x0002);
            }
        });
    }
}
